from django.shortcuts import render
from rest_framework import mixins
from rest_framework import viewsets
from .models import UserFav, UserLeavingMessage, UserAddress
from rest_framework.permissions import IsAuthenticated
from utils.permissions import IsOwnerOrReadOnly
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication
from rest_framework import permissions
from rest_framework import status
from rest_framework.views import Response

from .serializer import UserFavSerializer, UserFavDetailsSerializer, LeavingMessageSerialize, \
    UserAddress, UserAddressSerialize


class UserFavViewSte(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin,
                     mixins.DestroyModelMixin, viewsets.GenericViewSet):
    """
    create:
        用户收藏
    list:
        获取用户收藏
    destroy:
        判断某个商品是否被收藏
    destroy:
        删除用户收藏
    """

    def get_serializer_class(self):
        if self.action == 'list':  # 判断请求方式
            # 请求用list列表返详情
            return UserFavDetailsSerializer
        elif self.action == 'create':
            # 返回注册用户是Serializesr
            return UserFavSerializer
        return UserFavSerializer  # 其他情况默认返回用户详情

    # IsAuthenticated判断用户是否登陆，IsOwnerOrReadOnly我自定义的用户权限，只能修改自己用户的数据
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)  # 判断是否登陆和用户拥有的权限
    # 配置JSONWebTokenAuthentication认证模式，配置认内容：SessionAuthentication这玩意发给我就给认证
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)  # 配置持久化验证方式
    # 自定义查询字段
    lookup_field = 'goods_id'  # 可以直接在收藏用商品id，而不是用搜藏表的id

    # 重写get_queryset过滤出当前用户的收藏数据
    def get_queryset(self):
        print(self.request.user)
        return UserFav.objects.filter(user=self.request.user)


class LeavingMessageViewSte(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin,
                            mixins.DestroyModelMixin,
                            viewsets.GenericViewSet):
    """
    create:
        新建留言
    list:
        获取留言
    retrieve:
        留言详情
    destroy:
        删除留言


    """

    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    queryset = UserLeavingMessage.objects.all()
    serializer_class = LeavingMessageSerialize

    # 获取我的留言
    def get_queryset(self):
        return UserLeavingMessage.objects.filter(user=self.request.user)


class UserAddressViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin,
                         mixins.DestroyModelMixin,
                         viewsets.GenericViewSet):
    """
    list:
        用户收收件址列表
    create:
        创建用户收件地址
    retrieve:
        用户手收件地址详情
    destroy:
        删除用户收件地址
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    queryset = UserAddress.objects.all()
    serializer_class = UserAddressSerialize

    def create(self, request, *args, **kwargs):
        # 表单数据在request.data里
        serializer = self.get_serializer(data=request.data)  # get_serializer获取序列化表单并填入数据
        # 验证表单
        serializer.is_valid(raise_exception=True)  # raise_exception=True 验证失败直接抛异常
        user = serializer.validated_data['user']
        province = serializer.validated_data['province']
        city = serializer.validated_data['city']
        district = serializer.validated_data['district']
        address = serializer.validated_data['address']
        signer_name = serializer.validated_data['signer_name']
        signer_mobile = serializer.validated_data['signer_mobile']

        ua = UserAddress(user=user, province=province, city=city, district=district, address=address,
                         signer_name=signer_name, signer_mobile=signer_mobile)
        ua.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED, )

    def get_queryset(self):
        return UserAddress.objects.filter(user=self.request.user)
